package sim.ring.gui.treedata;

import java.util.ArrayList;
import java.util.List;

public class Category extends DataModel {
	
	protected List<TMethod> methods;
	
	protected IDataModelVisitor adder = new Adder();
	protected IDataModelVisitor remover = new Remover();
	
	public List<TMethod> getMethods() {
		return methods;
	}

	public Category() {
		this.methods = new ArrayList<TMethod>();
	}
	
	public Category(String categoryName) {
		this();
		this.name = categoryName;
	}
	
	public Category(String categoryName, int id) {
		this();
		this.name = categoryName;
		this.id = id;
	}
	
	protected void addMethod(TMethod newMethod) {
		methods.add(newMethod);
		newMethod.parent = this;
		fireAddEvent(newMethod);
	}
	
	protected void removeMethod(TMethod delMethod) {
		methods.remove(delMethod);
		delMethod.addChangeListener(DataModelChangeListener.getInstance());
		fireRemoveEvent(delMethod);
	}
	
	public void add(DataModel model) {
		model.accept(adder, this);
	}
	
	public void remove(DataModel model) {
		model.accept(remover, this);
	}

	@Override
	public void accept(IDataModelVisitor visitor, Object args) {
		visitor.visitCategory(this, args);
	}
	
	
	private static class Adder implements IDataModelVisitor {

		@Override
		public void visitTMethod(TMethod method, Object args) {
			((Category)args).addMethod(method);
		}

		@Override
		public void visitCategory(Category category, Object args) { }

		@Override
		public void visitFolder(Folder folder, Object args) { }
		
	}
	
	private static class Remover implements IDataModelVisitor {

		@Override
		public void visitTMethod(TMethod method, Object args) {
			((Category)args).removeMethod(method);
			method.addChangeListener(DataModelChangeListener.getInstance());
		}

		@Override
		public void visitCategory(Category category, Object args) { }

		@Override
		public void visitFolder(Folder folder, Object args) { }
		
	}
	
}
